package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Question;
import cn.wolfcode.luowowo.article.mapper.QuestionMapper;
import cn.wolfcode.luowowo.article.query.QuestionQuery;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public Question selectByPrimaryKey(Long id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        return question;
    }

    @Override
    public List<Question> selectAll() {
        List<Question> questions = questionMapper.selectAll();
        return questions;
    }

    @Override
    public Long saveOrUpdate(Question question) {
        question.setReleaseTime(new Date());
        question.setCreateTime(new Date());
        question.setLastUpdateTime(new Date());
        question.setState(0);
        questionMapper.insert(question);
        return null;
    }

    @Override
    public PageInfo<Question> query(QuestionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Question> questions = questionMapper.query(qo);
        return new PageInfo<>(questions);
    }

    @Override
    public List<Question> queryByViewTop3(Long id) {
        return null;
    }

    @Override
    public int queryAttention(Long id, Long questionId) {
        Integer attention = questionMapper.queryAttention(id,questionId);
        return attention;
    }

    @Override
    public void setAttention(Long questionId, Long id, Integer state) {
        if(state>0){
            questionMapper.removeAttention(questionId,id);
        }else{
            if(questionMapper.queryAttention(id,questionId)==0){
                questionMapper.addAttention(questionId,id);
            }
        }
    }

    @Override
    public void addviewnum(Long questionId, int num) {
        questionMapper.addviewnum(questionId,num);
    }
    @Override
    public void addreplynum(Long questionId, int num) {
        questionMapper.addreplynum(questionId,num);
    }
    @Override
    public void addthumbupnum(Long questionId, int num) {
        questionMapper.addthumbupnum(questionId,num);
    }
}
